from collections import OrderedDict
import time

class TTLCache:
    def __init__(self, max_size=100, ttl=300):
        self.cache = OrderedDict()
        self.max_size = max_size
        self.ttl = ttl

    def get(self, key):
        if key in self.cache:
            value, timestamp = self.cache[key]
            if time.time() - timestamp < self.ttl:
                return value
            else:
                del self.cache[key]
        return None

    def set(self, key, value):
        if key in self.cache:
            del self.cache[key]
        elif len(self.cache) >= self.max_size:
            self.cache.popitem(last=False)
        self.cache[key] = (value, time.time())

def testTTLCache():
    cache = TTLCache(max_size=5, ttl=60)
    cache.set('key1', 'value1')
    cache.set('key2', 'value2')
    print(cache.get('key1'))  # 输出：value1
    time.sleep(61)
    print(cache.get('key1'))  # 输出：None
if __name__=="__main__":
    testTTLCache()

